package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hq.fxh.CustomerView.switchbutton.SwitchButton;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.AdBannerInfo;
import com.hq.fxh.No3_TuiGuang.MyMoBan.ClearableEidtText;
import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;


/**
 * @author 大圣
 * @ClassName QQAdItem
 * @Description :
 * @Created 16/6/24
 * @Email qwqe601660957@163.com
 */
public class QQAdItem extends LinearLayout {

    private FrameLayout flQQPicAdd;
    private ImageButton ibClose;
    private TextView tvAdTypeName;
    /** */
    private ImageView ivAd;
    /**
     * 姓名
     */
    private ClearableEidtText etQQ;
    /**
     * 手机号
     */
    private ClearableEidtText etNickName;
    /**
     * 联系地址
     */
    private ClearableEidtText etDes;
    /**
     * 一键拨号
     */

    private SwitchButton sbSwitch;
    private TextView tvSwitchInfo;
    private AdBannerInfo adBannerInfo;
    private AdBannerInfo.AdvertDetailBean Advert_detail;

    public QQAdItem(Context context) {
        super(context);
        inflate(context, R.layout.item_qq_ad, this);
        findView();
        etNickName.setSingleLine(true);
        etNickName.setHorizontallyScrolling(true);
        etDes.setSingleLine(true);
        etDes.setHorizontallyScrolling(true);
        tvAdTypeName.setText("QQ广告");
      /*  merchantToolSysConfigPresenter = new MerchantToolSysConfigPresenter(this);
        merchantToolSysConfigPresenter.attath(context);*/
//        merchantToolSysConfigPresenter.getConfig2();
//        SpannableStringBuilder style = StringUtils.formatStyle(getContext().getResources().getColor(R.color.blue),"名片广告可上传头像，设置电话和联系地址，支持一键拨号。{0}","查看详情");

    }

    private SpannableString getClickableSpan(String des, final String url) {
        OnClickListener l = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("title", "QQ广告");
                bundle.putString("url", url);
                //     IntentUtils.openActivity(getContext(), WebActivity.class, bundle);
//                Toast.makeText(getContext(), "Click Success", Toast.LENGTH_SHORT).show();
            }
        };
        SpannableString spanableInfo = new SpannableString(des + "查看详情");
        int start = spanableInfo.toString().indexOf("查看详情");
        int end = spanableInfo.length();
        spanableInfo.setSpan(new Clickable(l), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanableInfo.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.new_red)), start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanableInfo;
    }


    class Clickable extends ClickableSpan implements OnClickListener {
        private final OnClickListener mListener;

        public Clickable(OnClickListener l) {
            mListener = l;
        }

        @Override
        public void onClick(View v) {
            mListener.onClick(v);
        }
    }

    public void findView() {

        flQQPicAdd = (FrameLayout) findViewById(R.id.flQQPicAdd);
        ibClose = (ImageButton) findViewById(R.id.ibClose);
        tvAdTypeName = (TextView) findViewById(R.id.tvAdTypeName);
        ivAd = (ImageView) findViewById(R.id.ivAd);
        etQQ = (ClearableEidtText) findViewById(R.id.etQQ);
        etNickName = (ClearableEidtText) findViewById(R.id.etNickName);
        etDes = (ClearableEidtText) findViewById(R.id.etDes);

        sbSwitch = (SwitchButton) findViewById(R.id.sbSwitch);
        tvSwitchInfo = (TextView) findViewById(R.id.tvSwitchInfo);
    }

    public void setAdInfo(final AdBannerInfo info) {
        adBannerInfo = info;
        Advert_detail = info.getAdvert_detail();
        if (Advert_detail != null) {
            etQQ.setText(Advert_detail.getQQ());
            etQQ.setFilters(new InputFilter[]{new EmojiFilter()});
            etNickName.setText(Advert_detail.getName());
            etNickName.setFilters(new InputFilter[]{new EmojiFilter()});
            etDes.setText(Advert_detail.getDes());
            etDes.setFilters(new InputFilter[]{new EmojiFilter()});
            //  ImageLoader.getInstance().load(getContext(), ivAd, Advert_detail.getPic());
            String pic = Advert_detail.getPic();
            if (pic == null || pic.equals("")) {

            } else {
                GlideUtils.loadImage(getContext(), pic, ivAd);

            }
        } else if (Advert_detail == null) {
            Advert_detail = new AdBannerInfo.AdvertDetailBean();
            info.setAdvert_detail(Advert_detail);
        }
        etQQ.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.setQQ(s.toString());
                info.setAdvert_detail(Advert_detail);
            }
        });
        etNickName.addTextChangedListener(new TextWatcher() {
            private int maxLen = 24;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = etNickName.getText();
                int len = editable.length();
                if (len > maxLen) {
                    ToastUtils.show(getContext(), "输入字符不能超过" + maxLen + "个");
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, maxLen);
                    etNickName.setText(newStr);
                    editable = etNickName.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);

                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.setName(s.toString());
                info.setAdvert_detail(Advert_detail);
            }
        });
        etDes.addTextChangedListener(new TextWatcher() {
            private int maxLen = 50;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = etDes.getText();
                int len = editable.length();
                if (len > maxLen) {
                    ToastUtils.show(getContext(), "输入字符不能超过" + maxLen + "个");
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, maxLen);
                    etDes.setText(newStr);
                    editable = etDes.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);

                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.setDes(s.toString());
                info.setAdvert_detail(Advert_detail);
            }
        });


        sbSwitch.setChecked(BooleanUtils.intToBool(info.getIs_carousel()));
        setSbSwitchText(sbSwitch.isChecked(), 0);
        sbSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                info.setIs_carousel(BooleanUtils.BoolToInt(isChecked));
//                    adInfo.setCheckShow(adInfo.getCheckShow() +1);
//                    setSbSwitchText(isChecked,adInfo.getCheckShow());
            }
        });


    }
//        LddConfig lddConfig = merchantToolSysConfigPresenter.getConfig();
//        if(lddConfig != null){
//            tvDes.setClickable(true);
//            tvDes.setText(getClickableSpan(lddConfig.getQQDisplayWords(),lddConfig.getQQLinkUrl()));
//            tvDes.setMovementMethod(LinkMovementMethod.getInstance());
//        }

    public void setSbSwitchText(boolean isCheck, int checkShow) {
        if (isCheck) {
            tvSwitchInfo.setTextColor(this.getResources().getColor(R.color.new_black));
            tvSwitchInfo.setTextSize(14);
            tvSwitchInfo.setText(R.string.ad_on_tip);
            if (checkShow > 0) {
                ToastUtils.show(getContext(), R.string.to_show);
            }

        } else {
            tvSwitchInfo.setTextColor(this.getResources().getColor(R.color.new_black));
            tvSwitchInfo.setTextSize(14);
            tvSwitchInfo.setText(R.string.ad_off_tip);
            if (checkShow > 0) {
                ToastUtils.show(getContext(), R.string.to_hide);
            }

        }
    }


    public void setOnClickListener(final OnClickListener listener) {
        ibClose.setOnClickListener(listener);
        ivAd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(flQQPicAdd);
            }
        });
        flQQPicAdd.setOnClickListener(listener);
    }

    public void setImage(String url) {
        //     ImageLoader.getInstance().load(getContext(), ivAd, url);
        GlideUtils.loadImage(getContext(), url, ivAd);

        if (adBannerInfo != null && adBannerInfo.getAdvert_detail() != null) {
            adBannerInfo.getAdvert_detail().setPic(url);
        } else {
            AdBannerInfo.AdvertDetailBean advertDetailBean = new AdBannerInfo.AdvertDetailBean();
            advertDetailBean.setPic(url);
            advertDetailBean.setName("");
            advertDetailBean.setQQ("");
            advertDetailBean.setDes("");
            adBannerInfo.setAdvert_detail(advertDetailBean);
        }
    }
}
